package com.thankcreate.geekchat.views;

import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.ViewPart;

import com.thankcreate.model.ContactEntry;
import com.thankcreate.model.ContactGroup;
import com.thankcreate.model.Contact;
import com.thankcreate.model.IContactListener;
import com.thankcreate.geekchat.editors.ChatEditor;
import com.thankcreate.geekchat.editors.ChatEditorInput;
import com.thankcreate.geekchat.providers.*;

public class ContactsView extends ViewPart {

	public static final String ID = "com.thankcreate.geekchat.ContactsView";
	
	private TreeViewer treeViewer;	
	
	public ContactsView() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void createPartControl(Composite parent) {	
			
		treeViewer = new TreeViewer(parent, SWT.BORDER | SWT.MULTI
				| SWT.V_SCROLL);
		initTreeViewer(treeViewer,parent);	

		ContactGroup root = new ContactGroup("root", "ss", 1, null);
		root.addListener(new IContactListener() {
			
			@Override
			public void contactsChanged() {
				// TODO Auto-generated method stub
				treeViewer.refresh();
			}
		});
		ContactGroup group1 = new ContactGroup("Group1", "ss", 1, root);
		ContactEntry aContactEntry = new ContactEntry("a", "s", 1, group1);
		ContactEntry bContactEntry = new ContactEntry("b", "s", 1, group1);
		ContactGroup group2 = new ContactGroup("Group2", "ss", 1, root);
		

		treeViewer.setInput(root);
	}

	public void initTreeViewer(TreeViewer treeViewer, Composite parent){
//		treeViewer = new TreeViewer(parent, SWT.BORDER | SWT.MULTI
//				| SWT.V_SCROLL);
		treeViewer.setAutoExpandLevel(2);
		treeViewer.addDoubleClickListener(new IDoubleClickListener() {
			
			@Override
			public void doubleClick(DoubleClickEvent event) {
				handleTreeDoubleClickEvent(event);
			}
		});
		getSite().setSelectionProvider(treeViewer);		
		treeViewer.setContentProvider(new MyContentProvider());
		treeViewer.setLabelProvider(new MyLableProvider());
	}
	
	public void handleTreeDoubleClickEvent(DoubleClickEvent event){
		ISelection selection = event.getSelection();
		if(selection instanceof IStructuredSelection){			
			IStructuredSelection iStructuredSelection = 
				(IStructuredSelection)selection;
			if(iStructuredSelection.getFirstElement() instanceof
					ContactEntry){
				handleLeafDoubleClickEventSelection(selection);
			}else if (iStructuredSelection.getFirstElement() instanceof
					ContactGroup) {
				handleNodeDoubleClickEventSelection(selection);				
			}
		}
	}
	
	public void handleLeafDoubleClickEventSelection(ISelection selection) {
		IStructuredSelection iStructuredSelection = (IStructuredSelection) selection;

		ContactEntry newContactEntry = (ContactEntry) iStructuredSelection
				.getFirstElement();
		ChatEditorInput input = new ChatEditorInput(newContactEntry);
		try {
			getSite().getPage().openEditor(input, ChatEditor.ID);
		} catch (PartInitException e) {
			e.printStackTrace();
		}

	}

	public void handleNodeDoubleClickEventSelection(ISelection selection){
		IStructuredSelection iStructuredSelection = (IStructuredSelection) selection;
		TreeItem[] items = treeViewer.getTree().getSelection();
		if(items == null || items.length == 0){
			return;
		}
		if(items[0].getExpanded()){
			items[0].setExpanded(false);	
		}else{
			items[0].setExpanded(true);
		}
		
	}
	@Override
	public void setFocus() {
		treeViewer.getControl().setFocus();

	}
}


